﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;


namespace School.Admission
{
    public class Test_Subject
    {

        private static SchoolEntities db = new SchoolEntities();
        
        public static Int64 Add(ADM_Test_Subject Test_Subject)
        {
            try
            {
                db.AddToADM_Test_Subject(Test_Subject);

                db.SaveChanges();
                return Test_Subject.Id;
            }
            catch
            {
                return -1;
            }
        }
        public static ADM_Test_Subject  Single(long id)
        {
            try
            {
                return db.ADM_Test_Subject.Single(x => x.Id == id);
            }
            catch
            {
                return null;
            }
        }


        public static IEnumerable<ADM_Test_Subject>  All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from TestSubject in db.ADM_Test_Subject
                        select TestSubject;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Test_Id")
                {
                    long Test_Id;
                    Test_Id = Convert.ToInt64(item.Value);
                    query = query.Where(c => c.Test_Id == Test_Id);
                }
                if (item.Key == "Name")
                {
                    String nam;
                    nam = item.Value.ToString();
                    query = query.Where(c =>  c.Name.Contains(nam));

                }
                if (item.Key == "Duration")
                {
                    long Duration;
                    Duration = Convert.ToInt32(item.Value);
                    query = query.Where(c => c.Duration == Duration);
                }

            }
            return query.ToList();
        }

        public static void Update(ADM_Test_Subject  TestSubject)
        {

            ADM_Test_Subject c = db.ADM_Test_Subject.Single(x => x.Id == TestSubject.Id);
            c.Test_Id = TestSubject.Test_Id ;
            c.Name = TestSubject.Name;

            c.Duration = TestSubject.Duration ;
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.ADM_Test_Subject.DeleteObject(db.ADM_Test_Subject.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IEnumerable <ADM_Test_Subject> GetTestSubject(int TestId)
        {
            var result = from x in db.ADM_Test_Subject 
                         where  x.Test_Id == TestId 
                         orderby x.Name 
                         select x;
            return result;
        }

    }
}
